extends NodeState


# 按住射门键时还是累积方向，直到松开射门键时，根据累积的方向归一化后作为最终的射门方向



var player: Player:
	get:
		return state_matchine.root as Player


var time_start_shot: int
# 最大的预备时间（毫秒）
var max_prepping_shot_time: float
# 持续时间中的射门方向向量的累积
var shot_direction: Vector2


func _on_process(delta : float) -> void:
	process_anmiation()
	process_preping_shot(delta)




func _on_enter(_data: NodeStateData) -> void:
	player.velocity = Vector2.ZERO

	time_start_shot = Time.get_ticks_msec()
	# 转换成毫秒单位
	max_prepping_shot_time = player.max_prepping_shot_bonus * 1000
	shot_direction = Vector2.ZERO



func _on_exit() -> void:
	pass



func process_preping_shot(delta : float):
	accumulate_shot_direction(delta)

	if player.input_manager.is_shoot_released():
		process_shoot()



func process_shoot():
	var duration_press := clampf(float(Time.get_ticks_msec() - time_start_shot), 0.0, max_prepping_shot_time)
	var ease_time := duration_press / max_prepping_shot_time
	var bonus := ease(ease_time, player.bonus_curve)
	var shot_power := player.shooting_power * (1 + bonus)

	var data := PlayerShootingData.new()
	data.shot_power = shot_power
	data.shot_direction = shot_direction.normalized()
	transition_state(Player.STATE_SHOOTING, data)



# 根据时间累计计算射门方向
func accumulate_shot_direction(delta : float):
	if player.input_manager.movement_direction == Vector2.ZERO:
		shot_direction += Vector2(player.character_orientation(), 0) * delta
	else:
		shot_direction += player.input_manager.movement_direction * delta



func process_anmiation():
	player.animation_player.play(Player.ANIMATION_PREP_KICK)
